<?php namespace Phpcmf\Controllers;

class Buybisai extends \Phpcmf\Home\Mform {
	// 继承类初始化
	protected function _Extend_Init() {
	    // 初始化模块
	    $this->_module_init(APP_DIR);
	
	    // 判断表单是否操作
	    $this->form = $this->module['form']['son'];
	    if (!$this->form) {
	        $this->_msg(0, dr_lang('模块表单00【%s】不存在',\Phpcmf\Service::L('Router')->class));
	    }
	
	    // 支持附表存储
	    $this->is_data = 1;
	    // 模板前缀(避免混淆)
	    $this->tpl_name = $this->form['table'];
	    $this->tpl_prefix = 'mform_';
	
	    // 初始化数据表
	    $this->_init([
	        'table' => dr_module_table_prefix(APP_DIR).'_form_'.$this->form['table'],
	        'field' => $this->form['field'],
	        'show_field' => 'title'
	    ]);

	}
	
	/**
	 * 提交支付账单
	 */
	public function bisaizhifu() {
	
	    if (IS_POST) {
	        $scorelog = \Phpcmf\Service::L('input')->post('pay');
	        $scorelog['uid'] = (int)$this->member['uid'];
	        $scorelog['username'] = (string)$this->member['username'];
	        $money = floatval($scorelog['money']);
	        if (!$money) {
	            $this->_msg(0, dr_lang('金额(%s)不正确', $money));
	            exit;
	        }
			//dr_debug('scorelog', $scorelog);
			
			/*$tianshus = \Phpcmf\Service::M()->db->table('1_'.$scorelog['s'].'_form_son')->where('id', $scorelog['quanid'])->get()->getRowArray();
			$tianshu = (int)$tianshus['youxiaoshijian'];
			if ($tianshu == 0) {
				$tianshu = 30000;
			} else {
				$tianshu = $tianshu;
			}*/
			$shijianid = \Phpcmf\Service::M()->db->table('1_'.$scorelog['s'].'_bisaifufei')->where('cid', $scorelog['id'])->where('uid', $this->uid)->get()->getRowArray();
			
			if ($shijianid) {
			    $this->_msg(0, dr_lang('该账单已支付'));
			} else {
				if ($this->member['money'] < $money) {
					$this->_msg(0, '余额不足了，请充值');
				}
				// 新增记录表
				\Phpcmf\Service::M()->db->table('1_'.$scorelog['s'].'_bisaifufei')->insert(array(
				    'cid' => $scorelog['id'],
				    'uid' => $this->uid,
					'inputtime' => SYS_TIME,
					'leixing' => 1,
					'endtime' => SYS_TIME,
				));
			}
			
			
			$urls = 'index.php?s='.$scorelog['s'].'&c=son&id='.$scorelog['id'];

	       \Phpcmf\Service::M('pay')->add_money($this->uid, -$money);
	       // 增加一条支出记录
	       \Phpcmf\Service::M('pay')->add_paylog([
	           'uid' => $this->uid,
	           'username' => '',
	           'touid' => '',
	           'tousername' => $scorelog['username'],
	           'mid' => 'system',
	           'title' => $scorelog['title'].'比赛支付',
	           'value' => -$money,
	           'type' => $scorelog['type'],
	           'status' => 1,
	           'result' => $scorelog['id'],
	           'paytime' => SYS_TIME,
	           'inputtime' => SYS_TIME,
	       ]);
	        $url = PAY_URL.'index.php?s='.$scorelog['s'].'&c=son&id='.$scorelog['id'];
	        if (IS_API_HTTP || (\Phpcmf\Service::L('input')->get('is_ajax') || IS_API_HTTP || IS_AJAX)) {
	            // 回调页面
	            $this->_msg(0, $url, '支付成功');
	        } else {
	            // 跳转到支付页面，必须跳转到统一的主域名中付款
	            dr_redirect($url, 'auto');
	        }
	        exit;
	    } else {
	        $this->_msg(0, dr_lang('POST请求错误'));
	    }
	}
	/**
	 * 提交积分支付账单
	 */
	public function bisaijifen() {
	
	        $id = \Phpcmf\Service::L('input')->get('id');
			

			$shijianid = \Phpcmf\Service::M()->db->table(dr_module_table_prefix(APP_DIR).'_form_son')->where('id', $id)->where('uid', $this->uid)->get()->getRowArray();
			$bisaijiangli = 0;
			$mcjl = dr_string2array($shijianid['mcjl']);
			foreach($mcjl as $value) {
				$bisaijiangli += intval($value[2]);
			}
			\Phpcmf\Service::M()->db->table('1_'.MOD_DIR.'_form_son')->where('id', $id)->update([
			    'bisaijiangli' => $bisaijiangli,
			]);
			if (!$shijianid){
				$this->_msg(0, '没有权限支付');
			}
			if ($this->member['score'] < $bisaijiangli) {
				$this->_msg(0, '您的 '.SITE_SCORE.' 是 '.$this->member['score'].' 不够了');
			}
			$shifouzhifu = \Phpcmf\Service::M()->db->table(dr_module_table_prefix(APP_DIR).'_bisaijifen')->where('cid', $id)->where('uid', $this->uid)->get()->getRowArray();
			
			if ($shifouzhifu) {
			    $this->_msg(0, dr_lang('该账单已支付'));
			} else {
				if ($this->member['money'] < $money) {
					$this->_msg(0, SITE_SCORE.'不足了');
				}
				// 新增记录表
				\Phpcmf\Service::M()->db->table(dr_module_table_prefix(APP_DIR).'_bisaijifen')->insert(array(
				    'cid' => $id,
				    'uid' => $this->uid,
					'inputtime' => SYS_TIME,
					'leixing' => 1,
					'endtime' => SYS_TIME,
				));
			}
			
			$urls = 'index.php?s='.APP_DIR.'&c=son&id='.id;
			\Phpcmf\Service::M('member')->add_score($this->uid, -$bisaijiangli, $note = '积分支付比赛', $urls, $mark = '', $count = 0, $id);
	       
	        $url = PAY_URL.'index.php?s='.APP_DIR.'&c=son&id='.$id;
	        if (IS_API_HTTP || (\Phpcmf\Service::L('input')->get('is_ajax') || IS_API_HTTP || IS_AJAX)) {
	            // 回调页面
	            $this->_msg(0, $url, '消费成功');
	        } else {
	            // 跳转到支付页面，必须跳转到统一的主域名中付款
	            dr_redirect($url, 'auto');
	        }
	        exit;
	    
	}


    public function index() {

        $id = (int)\Phpcmf\Service::L('Input')->get('id');
		
        if (!$id) {
            exit($this->_msg(0, dr_lang('id参数不完整')));
        } elseif (!$this->uid) {
            $this->_msg(0, dr_lang('登录之后才能付款'));
        }
		
		$dateid = \Phpcmf\Service::M()->table('1_'.MOD_DIR.'_form_son')->get($id);
		$bisaijiangli = 0;
		$mcjl = dr_string2array($dateid['mcjl']);
		foreach($mcjl as $value) {
			$bisaijiangli += intval($value[2]);
		}
		\Phpcmf\Service::M()->db->table('1_'.MOD_DIR.'_form_son')->where('id', $id)->update([
		    'bisaijiangli' => $bisaijiangli,
		]);
		//dr_debug('文件名称', $dateid);
        if (!$dateid) {
            $this->_msg(0, dr_lang('帖子id不存在'));
        } elseif (\Phpcmf\Service::M('buybisai', MOD_DIR)->is_buy($this->uid, $id)) {
			/*$tianshus = \Phpcmf\Service::M()->db->table('1_'.MOD_DIR.'_form_son')->where('id', $id)->get()->getRowArray();
			$tianshu = (int)$tianshus['yxsj'];
			if ($tianshu == 0) {
				$this->_msg(0, dr_lang('已经购买过，无需重复购买'));
			} else {
				
				
			}
			$daoqi = \Phpcmf\Service::M()->db->table('1_'.MOD_DIR.'_tupian_buy')->where('cid', $id)->where('uid', $this->uid)->get()->getRowArray();
			dr_debug('$daoqi', $daoqi);
			if ($daoqi['endtime'] < time()) {
				
			} else {
				$this->_msg(0, dr_lang('已经购买过，无需重复购买'));
			}*/
			$this->_msg(0, dr_lang('已经购买过，无需重复购买'));
				
			
        }
		$yue = dr_payform('my-'.MOD_DIR.'_buybisai-'.$id);
		//dr_debug('文件名称', $yue);

        \Phpcmf\Service::V()->assign([
            'payform' => dr_payform('my-'.MOD_DIR.'_buybisai-'.$id),
            'meta_title' => dr_lang('在线付款').SITE_SEOJOIN.SITE_NAME,
            'meta_keywords' => $this->get_cache('site', SITE_ID, 'config', 'SITE_KEYWORDS'),
            'meta_description' => $this->get_cache('site', SITE_ID, 'config', 'SITE_DESCRIPTION')
        ]);
        \Phpcmf\Service::V()->display('buywenda.html');
    }
	
	public function goumaidaan() {
	
	    $id = (int)\Phpcmf\Service::L('Input')->get('id');
		$zhongbiaoid = (int)\Phpcmf\Service::L('Input')->get('zhongbiaoid');
	    if (!$id) {
	        exit($this->_msg(0, dr_lang('id参数不完整')));
	    } elseif (!$this->uid) {
	        $this->_msg(0, dr_lang('登录之后才能付款'));
	    }
		
		$dateid = \Phpcmf\Service::M()->table('1_'.MOD_DIR.'_form_son')->get($id);
		//dr_debug('文件名称', $dateid);
	    if (!$dateid) {
	        $this->_msg(0, dr_lang('帖子id不存在'));
	    } elseif (\Phpcmf\Service::M('buybisaidaan', MOD_DIR)->is_buy($this->uid, $id)) {
			/*$tianshus = \Phpcmf\Service::M()->db->table('1_'.MOD_DIR.'_form_son')->where('id', $id)->get()->getRowArray();
			$tianshu = (int)$tianshus['yxsj'];
			if ($tianshu == 0) {
				$this->_msg(0, dr_lang('已经购买过，无需重复购买'));
			} else {
				
				
			}
			$daoqi = \Phpcmf\Service::M()->db->table('1_'.MOD_DIR.'_tupian_buy')->where('cid', $id)->where('uid', $this->uid)->get()->getRowArray();
			dr_debug('$daoqi', $daoqi);
			if ($daoqi['endtime'] < time()) {
				
			} else {
				$this->_msg(0, dr_lang('已经购买过，无需重复购买'));
			}*/
			$this->_msg(0, dr_lang('已经购买过，无需重复购买'));
				
			
	    }
		$yue = dr_payform('my-'.MOD_DIR.'_buybisaidaan-'.$id);
		//dr_debug('文件名称', $yue);
	
	    \Phpcmf\Service::V()->assign([
	        'payform' => dr_payform('my-'.MOD_DIR.'_buybisaidaan-'.$id),
	        'meta_title' => dr_lang('在线付款').SITE_SEOJOIN.SITE_NAME,
	        'meta_keywords' => $this->get_cache('site', SITE_ID, 'config', 'SITE_KEYWORDS'),
	        'meta_description' => $this->get_cache('site', SITE_ID, 'config', 'SITE_DESCRIPTION')
	    ]);
	    \Phpcmf\Service::V()->display('buywenda.html');
	}
	/**
	 * 提交支付账单
	 */
	public function wendadaanzhifu() {
	
	    if (IS_POST) {
	        $scorelog = \Phpcmf\Service::L('input')->post('pay');
	        $scorelog['uid'] = (int)$this->member['uid'];
	        $scorelog['username'] = (string)$this->member['username'];
	        $money = floatval($scorelog['money']);
	        if (!$money) {
	            $this->_msg(0, dr_lang('金额(%s)不正确', $money));
	            exit;
	        }
			//dr_debug('scorelog', $scorelog);
			
			/*$tianshus = \Phpcmf\Service::M()->db->table('1_'.$scorelog['s'].'_form_son')->where('id', $scorelog['quanid'])->get()->getRowArray();
			$tianshu = (int)$tianshus['youxiaoshijian'];
			if ($tianshu == 0) {
				$tianshu = 30000;
			} else {
				$tianshu = $tianshu;
			}*/
			$shijianid = \Phpcmf\Service::M()->db->table('1_'.$scorelog['s'].'_wendazhongbiao')->where('cid', $scorelog['id'])->where('uid', $this->uid)->where('leixing', 5)->get()->getRowArray();
			$tiwenuid = \Phpcmf\Service::M()->db->table('1_'.$scorelog['s'].'_form_son')->where('id', $scorelog['id'])->get()->getRowArray();
			if (!$tiwenuid) {
				$this->_msg(0, dr_lang('没有这个提问'));
			}
			$daanuid = \Phpcmf\Service::M()->db->table('1_'.$scorelog['s'].'_form_wenda')->where('id', $scorelog['zhongbiaoid'])->where('tid', $scorelog['id'])->get()->getRowArray();
			if (!$daanuid) {
				$this->_msg(0, dr_lang('没有这个答案'));
			}
			if ($shijianid) {
			    $this->_msg(0, dr_lang('该账单已支付'));
			} else {
				if ($this->member['money'] < $money) {
					$this->_msg(0, '余额不足了，请充值');
				}
				// 新增记录表
				\Phpcmf\Service::M()->db->table('1_'.$scorelog['s'].'_wendazhongbiao')->insert(array(
				    
					'cid' => $scorelog['id'],
					'leixing' => 5, //购买中标
					'zhongbiaoid' => $scorelog['zhongbiaoid'],
					'shenhe' => 1,
					'jiangli' => 0,
					'jianglirmb' => $money,
					'uid' => $this->uid,
					'inputtime' => SYS_TIME,
					'endtime' => SYS_TIME
				));
				
			}
			
			
			$urls = 'index.php?s='.$scorelog['s'].'&c=son&id='.$scorelog['id'];
	
	       \Phpcmf\Service::M('pay')->add_money($this->uid, -$money);
	       // 增加一条支出记录
	       \Phpcmf\Service::M('pay')->add_paylog([
	           'uid' => $this->uid,
	           'username' => '',
	           'touid' => '',
	           'tousername' => $scorelog['username'],
	           'mid' => 'system',
	           'title' => $tiwenuid['title'].'购买中标答案支付',
	           'value' => -$money,
	           'type' => $scorelog['type'],
	           'status' => 1,
	           'result' => $scorelog['id'],
	           'paytime' => SYS_TIME,
	           'inputtime' => SYS_TIME,
	       ]);
		   //给提问者奖励20%
		   $tiwenmoney = $money *0.2;
		   \Phpcmf\Service::M('pay')->add_money($tiwenuid['uid'], $tiwenmoney);
		   \Phpcmf\Service::M('pay')->add_paylog([
		       'uid' => $tiwenuid['uid'],
		       'username' => '',
		       'touid' => '',
		       'tousername' => '',
		       'mid' => 'system',
		       'title' => $tiwenuid['title'].'购买中标答案获得',
		       'value' => $tiwenmoney,
		       'type' => $scorelog['type'],
		       'status' => 1,
		       'result' => $scorelog['id'],
		       'paytime' => SYS_TIME,
		       'inputtime' => SYS_TIME,
		   ]);
		   //给中标这80%
		   $daanmoney = $money *0.8;
		   \Phpcmf\Service::M('pay')->add_money($daanuid['uid'], $daanmoney);
		   \Phpcmf\Service::M('pay')->add_paylog([
		       'uid' => $daanuid['uid'],
		       'username' => '',
		       'touid' => '',
		       'tousername' => '',
		       'mid' => 'system',
		       'title' => $tiwenuid['title'].'购买中标答案获得',
		       'value' => $daanmoney,
		       'type' => $scorelog['type'],
		       'status' => 1,
		       'result' => $scorelog['id'],
		       'paytime' => SYS_TIME,
		       'inputtime' => SYS_TIME,
		   ]);
		   
	        $url = PAY_URL.'index.php?s='.$scorelog['s'].'&c=son&id='.$scorelog['id'];
	        if (IS_API_HTTP || (\Phpcmf\Service::L('input')->get('is_ajax') || IS_API_HTTP || IS_AJAX)) {
	            // 回调页面
	            $this->_msg(0, $url, '支付成功');
	        } else {
	            // 跳转到支付页面，必须跳转到统一的主域名中付款
	            dr_redirect($url, 'auto');
	        }
	        exit;
	    } else {
	        $this->_msg(0, dr_lang('POST请求错误'));
	    }
	}
	
	
}
